/**
 * 
 */
package com.tomato.joke;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

/**
 * 所有activity基类
 * @author defier
 */
public abstract class BaseActivity  extends Activity {
	
	protected int whichTab = R.id.footer_btn_home;// 底部交换的tab
	
	protected ImageView footerHomeView;
	protected ImageView footerCloudView;
	protected ImageView footerFavoritesView;
	protected ImageView footerNewsView;
	protected ImageView footerSettingView;
	
	/**
	 * 初始化底栏视图
	 */
	protected void initFooterEvent() {
		footerHomeView = (ImageView)findViewById(R.id.footer_btn_home);
		footerHomeView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				redirectActivity(MainActivity.class);
				switchTabs(v);
			}
		});
		
		footerCloudView = (ImageView)findViewById(R.id.footer_btn_cloud);
		footerCloudView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				redirectActivity(MainActivity.class);
				switchTabs(v);
			}
		});
		
		footerFavoritesView = (ImageView)findViewById(R.id.footer_btn_favorites);
		footerFavoritesView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				redirectActivity(MainActivity.class);
				switchTabs(v);
			}
		});
		
		footerNewsView = (ImageView)findViewById(R.id.footer_btn_news);
		footerNewsView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				redirectActivity(MainActivity.class);
				switchTabs(v);
			}
		});
		
		footerSettingView = (ImageView)findViewById(R.id.footer_btn_setting);
		footerSettingView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				redirectActivity(MainActivity.class);
				switchTabs(v);
			}
		});
	}
	
	private void switchTabs(View v) {
		switch (v.getId()) {
		case R.id.footer_btn_home:
			footerHomeView.setImageResource(R.drawable.icon_home_focus);
			break;
		case R.id.footer_btn_cloud:
			footerCloudView.setImageResource(R.drawable.icon_cloud_focus);
			break;
		case R.id.footer_btn_favorites:
			footerFavoritesView.setImageResource(R.drawable.icon_favorites_focus);
			break;
		case R.id.footer_btn_news:
			footerNewsView.setImageResource(R.drawable.icon_news_focus);
			break;
		case R.id.footer_btn_setting:
			footerSettingView.setImageResource(R.drawable.icon_setting_focus);
			break;
		default:
			;
		}

		switch (whichTab) {
		case R.id.footer_btn_home:
			if (v.getId() != whichTab) {
				footerHomeView.setImageResource(R.drawable.icon_home);
			}
			break;
		case R.id.footer_btn_cloud:
			if (v.getId() != whichTab) {
				footerCloudView.setImageResource(R.drawable.icon_cloud);
			}
			break;
		case R.id.footer_btn_favorites:
			if (v.getId() != whichTab) {
				footerFavoritesView.setImageResource(R.drawable.icon_favorites);
			}
			break;
		case R.id.footer_btn_news:
			if (v.getId() != whichTab) {
				footerNewsView.setImageResource(R.drawable.icon_news);
			}
			break;
		case R.id.footer_btn_setting:
			if (v.getId() != whichTab) {
				footerSettingView.setImageResource(R.drawable.icon_setting);
			}
			break;
		default:
			;
		}
		whichTab = v.getId();
	}
	
	
	/**
	 * 查看详情
	 * 
	 * @param v
	 */
	@SuppressWarnings("rawtypes")
	private void redirectActivity(Class targetActivity) {
		try {
			Intent intent = new Intent();
			// 传递参数
			intent.setClass(this, targetActivity);
			startActivityForResult(intent, 0);
		} catch (Exception ex) {
			Log.e("intent", "error", ex);
		}
	}

}
